from django.db import models
from django.utils.translation import gettext_lazy  as _
from django.contrib.auth import get_user_model
import os
import uuid

User = get_user_model()

class Article(models.Model):
    """Article Model"""
    STATUS_CHOICES = (
        ('p', _('Published')),
        ('d', _('Draft')),
    )

    title = models.CharField(verbose_name=_('Title (*)'), max_length=90, db_index=True)
    body = models.TextField(verbose_name=_('Body'), blank=True)
    author = models.ForeignKey(User, verbose_name=_('Author'), on_delete=models.CASCADE, related_name='articles')
    status = models.CharField(_('Status (*)'), max_length=1, choices=STATUS_CHOICES, default='s', null=True, blank=True)
    create_date = models.DateTimeField(verbose_name=_('Create Date'), auto_now_add=True)

    # liked_by = models.ManyToManyField(to=User)

    def __str__(self):
        return self.title

    class Meta:
        ordering = ['-create_date']
        verbose_name = "Article"
        verbose_name_plural = "Articles"

def user_directory_path(instance, filename):
    ext = filename.split('.')[-1]
    filename = '{}.{}'.format(uuid.uuid4().hex[:10], ext)
    p=os.path.join("files", filename)
    print(p)
    return p

class FileUpload(models.Model):
    file = models.FileField(upload_to=user_directory_path, null=True)
    # name = models.CharField(max_length=64, verbose_name="file name")
    upname=models.CharField(max_length=128, verbose_name="upload file name")
    create_date = models.DateTimeField(verbose_name=_('Create Date'), auto_now_add=True)
    author = models.ForeignKey(User, verbose_name=_('Author'), on_delete=models.CASCADE, related_name='FileUpload')

    def __str__(self):
        return self.file.url
    

    class Meta:
        ordering = ['-create_date']
        verbose_name = "FileUpload"
        verbose_name_plural= verbose_name
